package com.gaoyi.crm.control;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.gaoyi.crm.callback.OnEndClickListener;

public class MyTextView extends AppCompatTextView {
    private Drawable mDrawable;

    public MyTextView(@NonNull Context context) {
        super(context);
        init();
    }

    public MyTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        mDrawable = getCompoundDrawablesRelative()[2];
        if (mDrawable != null) {
            mDrawable.setBounds(0, 0, mDrawable.getIntrinsicWidth(), mDrawable.getIntrinsicHeight());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP && mDrawable != null) {
            boolean touchable = event.getRawX() >= (getRight() - mDrawable.getBounds().width());
            if (touchable && onEndClickListener != null) {
                onEndClickListener.onEndClickListener();
            }
        }
        return true;
    }

    private OnEndClickListener onEndClickListener;

    public void setOnEndClickListener(OnEndClickListener onEndClickListener) {
        this.onEndClickListener = onEndClickListener;
    }
}
